package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.domain.PayOrder;
import cn.itsource.ymcc.dto.AlipayNotifyDto;
import cn.itsource.ymcc.dto.PayApplyDto;
import cn.itsource.ymcc.result.JSONResult;
import cn.itsource.ymcc.service.IPayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("pay")
public class PayController {
    @Autowired
    private IPayOrderService payOrderService;
    /**
     * 轮巡校验
     * 判断payorder 是否有订单号
     */

    @GetMapping("/checkPayOrder/{orderNo}")
    public JSONResult checkPayOrder(@PathVariable("orderNo") String orderNo){
        //判断payorder 里面是否保存好了 哪么就 查询payorder里面幼不幼稚
        PayOrder payOrder = payOrderService.selectByOrderNo(orderNo);
        //若果有值 哪么返回给前端，没有就 继续轮巡，直到payorder 保存到值
        return payOrder==null?JSONResult.error() : JSONResult.success();
    }
    /**
     * 轮巡结束，发起支付请求
     * post("/pay/pay/apply
     */
    @PostMapping("/apply")
    public JSONResult apply(@RequestBody PayApplyDto dto){
        ;// 向支付宝发起申请  返回from表单 这个表单时string 的类型
        String formStr= payOrderService.apply(dto);
        return JSONResult.success(formStr);
    }
    @RequestMapping("/alipay/notify")
    public String notify(AlipayNotifyDto dto) {
        try {
            System.out.println("支付宝异步回调。。。。。");
            return payOrderService.notify(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return "Fail";
        }
    }

}
